import java.util.Stack;

public class test3 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int x:pushed){
            stack.push(x);

            while(!stack.isEmpty() &&  popped[j] == stack.peek()){
                stack.pop();
                j++;
                if(j>=popped.length ){
                    return true;
                }
            }
        }
        //这个不一定判断，上面while循环的判断已经包括了
        // if(!stack.isEmpty() || j!=popped.length){
        //     return false;
        // }
        return false;
    }
}
